package Thread.Thread_Security;


public class Demo22 {
    private static Object locker1=new Object();
    private static Object locker2=new Object();

    public static void main(String[] args) {
        Thread t1=new Thread(()->{
           synchronized (locker1){
               System.out.println("t1对locker1加锁完成");
               //此处假的sleep是为了避免线程1立即执行完从而导致看不出两把锁两个对象造成的死锁的现象
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               synchronized (locker2){
                   System.out.println("t1对locker2加锁完成");
               }
           }
        });
        Thread t2=new Thread(()->{
           synchronized (locker2){
               System.out.println("t2对locker2加锁完成");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               synchronized (locker1){
                   System.out.println("t2对locker1加锁完成");
               }
           }

        });


        t1.start();
        t2.start();
    }
}
